﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GTAuto.DAL.DAL;
using GTAuto.DAL.Entities;
using GTAuto.DAL.Enums;

namespace GTauto.Site.FrontEnd.Controllers
{
    public class TiresController : Controller
    {
        //
        // GET: /Tires/
        public ActionResult Index()
        {
            int count; 
            List<GTBrand> gtBrands = DALBrands.Singleton.GetByType(BrandTypeEnum.Tires, 1, 10000, out count);

            var brandCol1 = new List<GTBrand>();
            var brandCol2 = new List<GTBrand>();
            var brandCol3 = new List<GTBrand>();

            int i = 1;
            foreach (var gtBrand in gtBrands)
            {
                if(i==1)
                    brandCol1.Add(gtBrand);

                if (i == 2)
                    brandCol2.Add(gtBrand);


                if (i == 3)
                {
                    brandCol3.Add(gtBrand);
                    i = 0;
                }
                i++;
            }

            ViewBag.brandCol1 = brandCol1;
            ViewBag.brandCol2 = brandCol2;
            ViewBag.brandCol3 = brandCol3;


            ViewBag.brandsT = new SelectList(gtBrands, "Id", "Name");

            gtBrands = DALBrands.Singleton.GetByType(BrandTypeEnum.Disc, 1, 10000, out count);

            ViewBag.brandsD = new SelectList(gtBrands, "Id", "Name");

            var brandCol1d = new List<GTBrand>();
            var brandCol2d = new List<GTBrand>();
            var brandCol3d = new List<GTBrand>();

            i = 1;
            foreach (var gtBrand in gtBrands)
            {
                if (i == 1)
                    brandCol1d.Add(gtBrand);

                if (i == 2)
                    brandCol2d.Add(gtBrand);


                if (i == 3)
                {
                    brandCol3d.Add(gtBrand);
                    i = 0;
                }
                i++;
            }

            ViewBag.brandCol1d = brandCol1d;
            ViewBag.brandCol2d = brandCol2d;
            ViewBag.brandCol3d = brandCol3d;


            ViewBag.TireWidth = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.TireWidth), "Code", "Value");
            ViewBag.TireProfile = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.TireProfile), "Code", "Value");
            ViewBag.TireDiameter = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.TireDiameter), "Code", "Value");
            ViewBag.TireSesons = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.TireSesons), "Code", "Value");


            ViewBag.DiscBoltCount = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.DiscBoltCount), "Code", "Value");
            ViewBag.DiscMezBolt = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.DiscMezBolt), "Code", "Value");
            ViewBag.DiscVilet = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.DiscVilet), "Code", "Value");
            ViewBag.DiscWidth = new SelectList(DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.DiscWidth), "Code", "Value");



            return View();
        }
        public ActionResult TireByBrand(int id,string ses)
        {
            int count;

            List<GTModel> models;

            models = ses == null ? DALModels.Singleton.GetByBrand(id, 1, 1000, out count) : DALModels.Singleton.GetByPropertyValue(id, Convert.ToInt32(DALSettngs.Singleton.Get("TireSeasonPropId").Value), ses == "1" ? DALSettngs.Singleton.Get("TireWinter").Value : DALSettngs.Singleton.Get("TireSummer").Value);

            if(ses==null)
            {
                ViewBag.All = "active";
            }
            else
            {
                if(ses=="1")
                {
                    ViewBag.Winter = "active";
                }
                else
                {
                    ViewBag.Summer = "active";
                }
            }


            ViewBag.Brand = DALBrands.Singleton.Get(id);

            return View(models);
        }
        public ActionResult TireByModel(int id)
        {
            ViewBag.All = "active";
            GTModel curModel = DALModels.Singleton.Get(id);
            int count;
            var models =  DALModels.Singleton.GetByBrand(curModel.BrandId, 1, 1000, out count);


            ViewBag.CurModel = curModel;
            ViewBag.Brand = DALBrands.Singleton.Get(curModel.BrandId);
            ViewBag.Models = models;
             

            List<GTModelModification> gtModelModifications = DALModelModification.Singleton.GetAll(id);
            ViewBag.Mods = gtModelModifications;



            return View(curModel);
        }

         [HttpPost]
         public  ActionResult  TireSearchResult(FormCollection formCollection)
         {
             var brandIdT = formCollection["BrandIdT"];
             var tireWidth = formCollection["tireWidth"];
             var tireProfile = formCollection["tireProfile"];
             var tireDiameter = formCollection["tireDiameter"];
             var tireSesons = formCollection["tireSesons"];


            
             string width = tireWidth == "-1" ?  null : tireWidth;
             string profile = tireProfile == "-1" ? null :tireProfile;
             string diam = tireDiameter == "-1" ? null : tireDiameter;
             string ses = DALDictionary.Singleton.GetPaged(DictionaryTypeEnum.TireSesons, tireSesons).Value;
             List<GTModel> models = DALTireDiscs.Singleton.TireModelsSearch(width, diam, profile, ses, null, Convert.ToInt32(brandIdT));

             ViewBag.Brand = DALBrands.Singleton.Get(Convert.ToInt32(brandIdT));


             return View(models);
         }


    }


    
}
